package pkg

import (
	"context"
	"fmt"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
	"shopping-mall/AppConf"

	"time"
)

func UpLode(file multipart.File, m *multipart.FileHeader) (ImageUrl string, err error) {
	accessKey := AppConf.AppConf.QiNiu.AK
	secretKey := AppConf.AppConf.QiNiu.SK
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := AppConf.AppConf.QiNiu.Bucket
	key := fmt.Sprintf("%d%s", time.Now().Unix(), m.Filename)

	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	_err := uploadManager.UploadReader(context.Background(), file, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
		FileName: key,
	}, nil)
	if _err != nil {
		return "", _err
	}
	return "http://svtgt6dq3.hn-bkt.clouddn.com/" + key, nil
}
